package com.github.iot.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.AppCompatEditText;

import com.github.iot.R;
import com.github.iot.dao.CommonDaoUtils;
import com.github.iot.dao.DaoUtilsStore;
import com.github.iot.entity.NetServer;
import com.github.iot.utils.BarUtils;
import com.github.iot.utils.CommonUtils;
import com.github.iot.utils.KeyboardUtils;
import com.github.iot.utils.ToastUtils;
import com.github.iot.view.RxTitle;
import com.github.iot.view.dialogs.iface.ISimpleDialogListener;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 设备服务端Activity
 *
 * @Author: 梁声洪
 * @Date: 2019/12/12 9:42
 * @Version: v1.0.0
 */
public class NetServerActivity extends AppCompatActivity implements ISimpleDialogListener {

    @BindView(R.id.rx_title)
    RxTitle mTitle;

    @BindView(R.id.et_ip)
    AppCompatEditText mIp;

    @BindView(R.id.et_port)
    AppCompatEditText mPort;

    private static NetServer netServer;
    private static CommonDaoUtils<NetServer> netServerDaoUtils = DaoUtilsStore.getInstance().getNetServerDaoUtils();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        BarUtils.setStatusBarColor(this, R.color.colorPrimaryDark);
        setContentView(R.layout.activity_net_server);

        ButterKnife.bind(this);
        initView();
        initData();
    }

    private void initView() {
        mTitle.setLeftFinish(this);
    }

    private void initData() {
        List<NetServer> list = netServerDaoUtils.queryAll();
        if (list.size() == 1) {
            netServer = list.get(0);

            mIp.setText(netServer.getIp());
            mPort.setText(String.valueOf(netServer.getPort()));
        }
    }

    private void netServer() {
        String ip = mIp.getText().toString();
        String port = mPort.getText().toString();

        if (TextUtils.isEmpty(ip)) {
            ToastUtils.warning("IP地址不能为空");
            return;
        }

        if (!CommonUtils.isIp(ip)) {
            ToastUtils.warning("IP地址输入有误！");
            return;
        }

        if (TextUtils.isEmpty(ip)) {
            ToastUtils.warning("端口不能为空");
            return;
        }

        if (Integer.valueOf(port) >= 65535 || Integer.valueOf(port) <= 0) {
            ToastUtils.warning("端口输入有误！");
            return;
        }

        if (null == netServer) {
            NetServer netServer = new NetServer();
            netServer.setIp(ip);
            netServer.setPort(Integer.valueOf(port));

            long i = netServerDaoUtils.insert(netServer);
            if (i > 0) {
                ToastUtils.success("操作成功");
                finish();
            } else {
                ToastUtils.warning("操作失败");
            }
        } else {
            netServer.setIp(ip);
            netServer.setPort(Integer.valueOf(port));

            boolean flag = netServerDaoUtils.update(netServer);
            if (flag) {
                ToastUtils.success("操作成功");
                finish();
            } else {
                ToastUtils.warning("操作失败");
            }
        }
    }

    @OnClick({R.id.btn_net_server})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_net_server:
                KeyboardUtils.hideSoftInput(NetServerActivity.this);
                netServer();
                break;
            default:
                break;
        }
    }

    @Override
    public void onNegativeButtonClicked(int requestCode) {

    }

    @Override
    public void onNeutralButtonClicked(int requestCode) {

    }

    @Override
    public void onPositiveButtonClicked(int requestCode) {

    }

}